package com.newy.core.hibernate;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
 * 统一定义id的entity基类.
 * 
 * 基类统一定义id的属性名称、数据类型、列名映射及生成策略.
 * 子类可重载getId()函数重定义id的列名映射和生成策略.
 * 
 * 代码来自： springside
 */
@MappedSuperclass
public class IdEntity {
	protected Long id;


	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE)
	//@GeneratedValue(generator = "system-uuid")
	//@GenericGenerator(name = "system-uuid", strategy = "uuid")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public boolean equals(Object obj){
		if (obj == null)
			return false;
		if (!(obj instanceof IdEntity))
			return false;
		
		IdEntity entity = (IdEntity)obj;
		if (!entity.getClass().equals(this.getClass()))
			return false;
		
		return entity.getId() == this.getId();
	}
	
	public String toString(){
		return this.getClass().getName() + "(id = " + id + ")";
	}
}
